package utils;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbUtil {
    private static String dbDriver = "com.mysql.jdbc.Driver";
    private static String dbUrl = "jdbc:mysql://182.92.215.233:3306/java_test?characterEncoding=utf8";
    private static String dbUser = "java_test";
    private static String dbPassword = "tsENCJecP2TZZRkM";
    public static Connection con = null;

    public static void setCon() {
        try {
            Class.forName(dbDriver);
            con = (Connection) DriverManager.getConnection(dbUrl, dbUser, dbPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws Exception {
        Connection con = null;
        try {
            Class.forName(dbDriver);
            con = (Connection) DriverManager.getConnection(dbUrl, dbUser, dbPassword);
        } catch (Exception e) {
//            e.printStackTrace();
            throw new Exception("数据库连接出现了问题");
        }
        return con;
    }

    public void closeConnection() throws Exception {
        if (con != null) {
            con.close();
        }
    }
}
